package com.it.common.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import com.yili.common.R;

public class CustomProgressBar extends View {
    private Paint mPaint;
    private int mProgressBackgroundColor;
    private int mProgressColor;
    private int mProgress;
    private int mMaxProgress;
    private Drawable mThumbDrawable;
    private float mThumbRadius;

    public CustomProgressBar(Context context) {
        this(context, null);
    }

    public CustomProgressBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs);
    }

    private void init(AttributeSet attrs) {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mThumbRadius = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, getResources().getDisplayMetrics());

        TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.CustomProgressBar, 0, 0);
        mProgressColor = a.getColor(R.styleable.CustomProgressBar_progressColor, Color.BLUE);
        mProgressBackgroundColor = a.getColor(R.styleable.CustomProgressBar_progressBackgroundColor, Color.GRAY);
        mThumbDrawable = a.getDrawable(R.styleable.CustomProgressBar_thumbDrawable);
        a.recycle();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int desiredHeight = (int) (mThumbRadius * 2) + getPaddingTop() + getPaddingBottom();
        int height = resolveSize(desiredHeight, heightMeasureSpec);

        int desiredWidth = MeasureSpec.getSize(widthMeasureSpec);
        int width = resolveSize(desiredWidth, widthMeasureSpec);

        setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // 绘制进度条背景
        mPaint.setColor(mProgressBackgroundColor);
        canvas.drawLine(mThumbRadius, getHeight() / 2, getWidth() - mThumbRadius, getHeight() / 2, mPaint);

        // 绘制当前进度
        mPaint.setColor(mProgressColor);
        float thumbPos = (mProgress * (getWidth() - 2 * mThumbRadius)) + mThumbRadius;
        canvas.drawLine(mThumbRadius, getHeight() / 2, thumbPos, getHeight() / 2, mPaint);

        // 绘制移动小球
        if (mThumbDrawable != null) {
            mThumbDrawable.setBounds((int) (thumbPos - mThumbRadius), 0, (int) (thumbPos + mThumbRadius), getHeight());
            mThumbDrawable.draw(canvas);
        } else {
            mPaint.setColor(Color.WHITE);
            canvas.drawCircle(thumbPos, getHeight() / 2, mThumbRadius, mPaint);
        }
    }

    public void setProgress(int progress) {
        mProgress = progress;
        invalidate();
    }

    public void setMaxProgress(int maxProgress) {
        mMaxProgress = maxProgress;
    }

    public int getProgress() {
        return mProgress;
    }

    public int getMaxProgress() {
        return mMaxProgress;
    }

    public void setProgressColor(int color) {
        mProgressColor = color;
        invalidate();
    }

    public void setProgressBackgroundColor(int color) {
        mProgressBackgroundColor = color;
        invalidate();
    }

    public void setThumbDrawable(Drawable drawable) {
        mThumbDrawable = drawable;
        invalidate();
    }
}
